package com.ruoyi.api.controller.user;

import com.ruoyi.api.annotation.CurrentUser;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.deal.domain.HyDealCollect;
import com.ruoyi.deal.domain.HyDealGoods;
import com.ruoyi.deal.service.IHyDealCollectService;
import com.ruoyi.system.domain.HyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 用户收藏API
 * @date 2021-06-03 18:34
 */
@RestController
@RequestMapping("/api/user")
public class ApiCollectController extends BaseController {

	@Autowired
	private IHyDealCollectService collectService;


	/**
	 * 获取用户收藏列表
	 */
	@GetMapping("/collect")
	public AjaxResult collect(@CurrentUser HyUser user) {

		List<HyDealGoods> collectList = collectService.queryGoodsListByUserId(user.getUserId());
		return AjaxResult.success(collectList);
	}

	/**
	 * 用户商品收藏
	 */
	@PostMapping("/collect")
	public AjaxResult collect(@CurrentUser HyUser user,@RequestBody HyDealGoods goods) {

		collectService.saveCollect(user.getUserId(), goods.getGoodsId());
		return AjaxResult.success("收藏成功");
	}

	/**
	 * 查询用户是否收藏
	 */
	@GetMapping("/iscollect/{goodsId}")
	public AjaxResult isCollect(@CurrentUser HyUser user,
							 @PathVariable("goodsId") Integer goodsId) {

		Boolean idCollect = collectService.isCollect(user.getUserId(), goodsId);

		return AjaxResult.success(idCollect);
	}


	/**
	 * 用户删除收藏
	 */
	@DeleteMapping("/collect/{goodsId}")
	public AjaxResult delete(@CurrentUser HyUser user,
							 @PathVariable("goodsId") Integer goodsId) {

		collectService.removeCollectByUserIdAndGoodsId(user.getUserId(), goodsId);
		return AjaxResult.success("删除收藏成功");
	}

}
